package com.magicalcoder.youyamvc.core.common.utils;

import com.magicalcoder.youyamvc.core.common.utils.date.DateFormatUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by hzhedongyu on 2016/8/12.
 */
public class POIUtil {
    public static void writeExcelToTempFile(List pageList,String[] titles,File tmpFile) {
        FileOutputStream out = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
        Sheet sh = wb.createSheet();
        try {
            //头部
            Row headRow = sh.createRow(0);
            for(int i=0;i<titles.length;i++){
                Cell cell = headRow.createCell(i);
                cell.setCellValue(titles[i]);
            }
            //数据
            for(int i=1;i<=pageList.size();i++){
                Object obj = pageList.get(i-1);
                Row row = sh.createRow(i);
                Field[] fields=obj.getClass().getDeclaredFields();
                for(int j=0;j<fields.length;j++){
                    Field v = fields[j];
                    Cell cell = row.createCell(j);
                    v.setAccessible(true);
                    Object va = v.get(obj);
                    String showValue;
                    if(va != null){
                        if(v.getType() == Date.class){
                            showValue = DateFormatUtils.getStringDate((Date)va,"yyyy-MM-dd HH:mm:ss");
                            cell.setCellValue(showValue);
                        }else if(v.getType() == Boolean.class){
                            if((Boolean) va){
                                showValue = "是";
                            }else {
                                showValue = "否";
                            }
                            cell.setCellValue(showValue);
                        }else if(v.getType() == Long.class){
                            cell.setCellValue((Long)va);
                        }else if(v.getType() == Integer.class){
                            cell.setCellValue((Integer)va);
                        }else if(v.getType() == Double.class){
                            cell.setCellValue((Double)va);
                        }else if(v.getType() == Float.class){
                            cell.setCellValue((Float)va);
                        }else if(v.getType() == BigDecimal.class){
                            cell.setCellValue(((BigDecimal)va).floatValue());
                        }else {
                            showValue = va+"";
                            cell.setCellValue(showValue);
                        }
                    }
                }
            }
            out = new FileOutputStream(tmpFile);
            wb.write(out);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(out!=null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(wb!=null){
                wb.dispose();
            }
        }
    }
}
